package com.sugar.psych.assessment.module.business.service.avatarinfo;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.sugar.psych.assessment.module.business.controller.admin.avatarinfo.vo.*;
import com.sugar.psych.assessment.module.business.dal.dataobject.avatarinfo.AvatarInfoDO;
import com.sugar.psych.assessment.framework.common.pojo.PageResult;
import com.sugar.psych.assessment.framework.common.pojo.PageParam;
import com.sugar.psych.assessment.framework.common.util.object.BeanUtils;

import com.sugar.psych.assessment.module.business.dal.mysql.avatarinfo.AvatarInfoMapper;

import static com.sugar.psych.assessment.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.sugar.psych.assessment.framework.common.util.collection.CollectionUtils.convertList;
import static com.sugar.psych.assessment.framework.common.util.collection.CollectionUtils.diffList;
import static com.sugar.psych.assessment.module.business.enums.ErrorCodeConstants.*;

/**
 * 随机头像 Service 实现类
 *
 * @author Ysugar
 */
@Service
@Validated
public class AvatarInfoServiceImpl implements AvatarInfoService {

    @Resource
    private AvatarInfoMapper avatarInfoMapper;

    @Override
    public Long createAvatarInfo(AvatarInfoSaveReqVO createReqVO) {
        // 插入
        AvatarInfoDO avatarInfo = BeanUtils.toBean(createReqVO, AvatarInfoDO.class);
        avatarInfoMapper.insert(avatarInfo);
        // 返回
        return avatarInfo.getId();
    }

    @Override
    public void updateAvatarInfo(AvatarInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateAvatarInfoExists(updateReqVO.getId());
        // 更新
        AvatarInfoDO updateObj = BeanUtils.toBean(updateReqVO, AvatarInfoDO.class);
        avatarInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteAvatarInfo(Long id) {
        // 校验存在
        validateAvatarInfoExists(id);
        // 删除
        avatarInfoMapper.deleteById(id);
    }

    @Override
        public void deleteAvatarInfoListByIds(List<Long> ids) {
        // 校验存在
        validateAvatarInfoExists(ids);
        // 删除
        avatarInfoMapper.deleteByIds(ids);
        }

    private void validateAvatarInfoExists(List<Long> ids) {
        List<AvatarInfoDO> list = avatarInfoMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(AVATAR_INFO_NOT_EXISTS);
        }
    }

    private void validateAvatarInfoExists(Long id) {
        if (avatarInfoMapper.selectById(id) == null) {
            throw exception(AVATAR_INFO_NOT_EXISTS);
        }
    }

    @Override
    public AvatarInfoDO getAvatarInfo(Long id) {
        return avatarInfoMapper.selectById(id);
    }

    @Override
    public PageResult<AvatarInfoDO> getAvatarInfoPage(AvatarInfoPageReqVO pageReqVO) {
        return avatarInfoMapper.selectPage(pageReqVO);
    }

}